import {Injectable} from '@nestjs/common';
// ORM
import {InjectRepository} from '@nestjs/typeorm';
import {Repository, Between} from 'typeorm';
import {LogListEntity} from '../entity/log.list.entity';
import {LogListDto} from '../dto/log.list.dto';

@Injectable()
export class LogListService {
    constructor(
        @InjectRepository(LogListEntity)
        private readonly logRepository: Repository<LogListEntity>,
    ) {
    }

    async createOne(logListDto: LogListDto): Promise<LogListEntity> {
        return await this.logRepository.save(logListDto);
    }

    async getLogListByTimeRange({startTime, endTime, pageIndex, pageSize}): Promise<[LogListEntity[], number]> {
        const qb = this.logRepository.createQueryBuilder();
        qb.where({
            time:Between(startTime, endTime),
        })
        qb.offset((pageIndex - 1) * pageSize)
            .limit(pageSize)
        qb.orderBy({
            time: 'DESC'
        })
        return qb.getManyAndCount();
    }
}
